//
//  MainTabBarViewController.swift
//  iOSProjectExtensionSwift
//
//  Created by HBGG on 2020/7/3.
//  Copyright © 2020 lidianchao. All rights reserved.
//

import UIKit

class MainTabBarViewController: UITabBarController,UITabBarControllerDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()
        newTabbarPush()
        layoutViewControllers()
        self.view.backgroundColor = .white
        self.tabBar.tintColor = kMainColor//设置tabbar选中标题颜色
        // Do any additional setup after loading the view.
    }
    private func newTabbarPush() {
        let baseTabBar = PushTabbar.init()
        self.setValue(baseTabBar, forKey: "tabBar")
    }
    ///创建Controllers
    private func layoutViewControllers() {
        self.viewControllers=MainTabBarManager.instance.getAllNavigationControllers().allNavs
    }
    //获取当前屏幕显示的viewcontroller
    private func getCurrentVC() -> UIViewController {
        let rootViewController = UIApplication.shared.windows[0].rootViewController
        let currentVC = getCurrentVCFrom(rootVC:rootViewController!)
        return currentVC
    }
    private func getCurrentVCFrom(rootVC:UIViewController) -> UIViewController {
        var currentVC = UIViewController()
        var newRootVc = rootVC
        
        if((rootVC.presentedViewController) != nil)  {
            // 视图是被presented出来的
            newRootVc = rootVC.presentedViewController!
            if(currentVC.isKind(of: UIViewController.self)) {
                newRootVc = currentVC
            }
        }
        if(newRootVc.isKind(of: UITabBarController.self)) {
            // 根视图为UITabBarController
            let tabBarVc = rootVC as! UITabBarController
            currentVC = getCurrentVCFrom(rootVC:tabBarVc.selectedViewController!)
        } else if (newRootVc.isKind(of: UINavigationController.self)) {
            // 根视图为UINavigationController
            let NaviVc = rootVC as! UINavigationController
            currentVC = getCurrentVCFrom(rootVC:NaviVc.visibleViewController!)
        } else {
            // 根视图为非导航类
            currentVC = newRootVc;
        }
        return currentVC

    }
    func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
        if(viewController == self.viewControllers![0] || viewController == self.viewControllers![1]) {
            return true
        }
        return true
    }
    //退出登录返回到主界面
    public func logutPopRootViewController() {
        getCurrentVC().navigationController?.popToRootViewController(animated: false)
        self.selectedIndex = 0
    }
    //登录
    public func goLogin() {
//        let loginVC = login
    }
    
    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}
